package com.cypher.encryptionanddecryption.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.bean.OtaBean;
import com.cypher.encryptionanddecryption.fragment.maintain.OtaProgramDownloadFragment;

/**
 * 空的模板视图
 */
public class EmptyActivity extends AppCompatActivity {

    private Button returnBtn;
    private TextView tabTitle;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_empty);

        init();
        initFragment();
        setListener();
    }

    private void init() {
        returnBtn = findViewById(R.id.empty_top_return);
        tabTitle = findViewById(R.id.empty_top_title);

        intent = getIntent();
        String title = intent.getStringExtra("title");
        if (title.length() > 15) {
            title = title.substring(0, 15) + "...";
        }
        tabTitle.setText(title);
    }

    private void initFragment() {
        String dataType = intent.getStringExtra("dataType");
        OtaBean bean = JSONObject.parseObject(intent.getStringExtra("data"), OtaBean.class);
        switch (dataType) {
            case "OTA":
                OtaProgramDownloadFragment otaProgramDownloadFragment = new OtaProgramDownloadFragment(bean);
                getSupportFragmentManager().beginTransaction().add
                        (R.id.empty_fragment, otaProgramDownloadFragment).commitAllowingStateLoss();
                getSupportFragmentManager().beginTransaction().show(otaProgramDownloadFragment).commitAllowingStateLoss();
                break;
        }
    }

    private void setListener() {
        View.OnClickListener returnBtnClick = (v) -> {
            this.finish();
        };
        returnBtn.setOnClickListener(returnBtnClick);
    }
}